package api

import (
	"context"
	"myproject/backend/types"
	"myproject/backend/utils"
)

type DeptApi struct {
	Ctx context.Context
}

func NewDeptApi() *DeptApi {
	return &DeptApi{}
}

func (c *DeptApi) CreateOrganizeDepartment(req types.CreateOrganizeDepartmentReqForApi) types.JsResp {
	resp := types.JsResp{}
	body := make(map[string]any)
	body["parentId"] = req.ParentId
	body["departmentName"] = req.DepartmentName
	body["remark"] = req.Remark
	body["organId"] = req.OrganId
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/customer/department/createOrganizeDepartment",
		Resp:   &resp,
		Body:   body,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

func (c *DeptApi) UpdateOrganizeDepartment(req types.UpdateOrganizeDepartmentReqForApi) types.JsResp {
	resp := types.JsResp{}
	body := make(map[string]any)
	body["id"] = req.Id
	body["departmentName"] = req.DepartmentName
	body["remark"] = req.Remark
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/customer/department/updateOrganizeDepartment",
		Resp:   &resp,
		Body:   body,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

func (c *DeptApi) GetOrganizeDepartmentById(req types.GetOrganizeDepartmentByIdReqForApi) types.GetOrganizeDepartmentByIdRespForApiResp {
	resp := types.GetOrganizeDepartmentByIdRespForApiResp{}
	query := make(map[string]any)
	query["id"] = req.Id
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method:      "GET",
		Suffix:      "/customer/department/getOrganizeDepartmentById",
		QueryParams: query,
		Resp:        &resp,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

func (c *DeptApi) DeleteOrganizeDepartmentById(req types.DeleteOrganizeDepartmentByIdReqForApi) types.JsResp {
	resp := types.JsResp{}
	body := make(map[string]any)
	body["id"] = req.Id
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/customer/department/deleteOrganizeDepartmentById",
		Resp:   &resp,
		Body:   body,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

func (c *DeptApi) CreatePositionForDepartment(req types.CreatePositionForDepartmentReqForApi) types.JsResp {
	resp := types.JsResp{}
	body := make(map[string]any)
	body["departmentId"] = req.DepartmentId
	body["departmentName"] = req.DepartmentName
	body["positionName"] = req.PositionName
	body["positionId"] = req.PositionId
	body["organId"] = req.OrganId
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/customer/department/createPositionForDepartment",
		Resp:   &resp,
		Body:   body,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

func (c *DeptApi) DeletePositionForDepartment(req types.DeletePositionForDepartmentReqForApi) types.JsResp {
	resp := types.JsResp{}
	body := make(map[string]any)
	body["departmentId"] = req.DepartmentId
	body["positionId"] = req.PositionId
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/customer/department/deletePositionForDepartment",
		Resp:   &resp,
		Body:   body,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

// @doc  "用户自定义可分配菜单列表"
// @handler OrganMenuDistributionList
// post /organMenuDistributionList (GetOrganMenuDistributionListReq) returns (GetOrganMenuDistributionListResp)
func (c *DeptApi) GetOrganMenuDistributionListReq(req types.GetOrganMenuDistributionListReq) types.GetOrganMenuDistributionListResp {
	resp := types.GetOrganMenuDistributionListResp{}
	body := make(map[string]any)
	body["departmentId"] = req.DepartmentId
	body["positionId"] = req.PositionId
	body["organId"] = req.OrganId
	body["domValue"] = req.DomValue
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/ras/configuration/organMenuDistributionList",
		Body:   body,
		Resp:   &resp,
		Prefix: "https://pbms.supore.cn",
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

func (c *DeptApi) CreateOrganMenuReq(req types.CreateOrganMenuReq) types.JsResp {
	resp := types.JsResp{}
	body := make(map[string]any)
	body["sourceIds"] = req.SourceIds
	body["departmentId"] = req.DepartmentId
	body["positionId"] = req.PositionId
	body["organId"] = req.OrganId
	body["domValue"] = req.DomValue
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Prefix: "https://pbms.supore.cn",
		Method: "POST",
		Suffix: "/ras/configuration/createOrganMenu",
		Body:   body,
		Resp:   &resp,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

// @doc "用户自定义功能权限列表"
// @handler OrganApiDistributionList
// post /organApiDistributionList (GetOrganApiDistributionListReq) returns (GetOrganApiDistributionListResp)
func (c *DeptApi) GetOrganApiDistributionListReq(req types.GetOrganApiDistributionListReq) types.GetOrganApiDistributionListResp {
	resp := types.GetOrganApiDistributionListResp{}
	body := make(map[string]any)
	body["departmentId"] = req.DepartmentId
	body["positionId"] = req.PositionId
	body["organId"] = req.OrganId
	body["domValue"] = req.DomValue
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Prefix: "https://pbms.supore.cn",
		Method: "POST",
		Suffix: "/ras/configuration/organApiDistributionList",
		Body:   body,
		Resp:   &resp,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

// @doc  "用户自定义功能权限配置"
// @handler CreateOrganApi
// post /createOrganApi (CreateOrganApiReq) returns ()
func (c *DeptApi) CreateOrganApiReq(req types.CreateOrganApiReq) types.JsResp {
	resp := types.JsResp{}
	body := make(map[string]any)
	body["sourceId"] = req.SourceId
	body["departmentId"] = req.DepartmentId
	body["positionId"] = req.PositionId
	body["organId"] = req.OrganId
	body["domValue"] = req.DomValue
	body["isDistribution"] = req.IsDistribution
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Prefix: "https://pbms.supore.cn",
		Method: "POST",
		Suffix: "/ras/configuration/createOrganApi",
		Body:   body,
		Resp:   &resp,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}
